package com.foodservice.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class DataCache{
	private String configFileName = "config.properties";
	private Map configMap = new HashMap<String,String>();
	private static DataCache instance = null;
	public static DataCache getInstance()
	{
		return null != instance ? instance : new DataCache();
	}
	
	public String getStringConfig(String key)
	{
		String str = (String)configMap.get(key);
		return null != str ? str : "";
	}
	
	public void refresh()
	{
		init();
	}
	
	
	private DataCache() {
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		loadConfig();
	}

	private void loadConfig() {
		// TODO Auto-generated method stub
		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getClassLoader()
					.getResourceAsStream(configFileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Set<Entry<Object, Object>> entrys = prop.entrySet();
		for (Entry e : entrys) {
			configMap.put(e.getKey(), e.getValue());
		}
	}
}
